home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / uip / other / rcvalert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-11  |  5.7 KB  |  263 lines

  1. #
  2. /*
  3.  *  RCVALERT personal receive-mail program
  4.  *
  5.  *  NOTE:  This is a USER program & is provided as a demonstration
  6.  *         of an MMDF feature.  It is NOT part of the MMDF "package".
  7.  *
  8.  *  Notifies user of new mail, if user logged in, by printing scan line
  9.  *  for the message on the user's terminal.
  10.  *
  11.  *  Does NOT actually deliver message.
  12.  *
  13.  *  Oct/79 David Crocker  Dept. of E.E., Univ. of Delaware
  14.  *  Jun/80 David Crocker  Notify ALL tty's logged in as recipient
  15.  *  Dec/80 David Crocker  Convert to stdio & v7
  16.  *  Apr/83 Doug Kingston  Modified for BRL (mostly fixes)
  17.  *  Apr/84 Doug Kingston  Modified for use as filter (rcvmail expunged)
  18.  */
  19. /* */
  20.  
  21. #undef    STDALONE /* run standalone, for debugging        */
  22.  
  23. #include "util.h"
  24. #include "mmdf.h"
  25. #include <pwd.h>
  26.  
  27. #define TOSHORT 16          /* subject shorter than this => search  */
  28.                   /*   body for chars to add to subject   */
  29. #define SUBLEN  37          /* length of subject & from fields      */
  30. #define FROMLEN 15
  31. #define UNSET   0          /* nothing added to subject from body   */
  32. #define SET     1          /* something "    "    "     "    "     */
  33.  
  34. extern LLog *logptr;
  35. extern char *compress();
  36. extern FILE *tty_fp;
  37.  
  38. char    inbuf[BUFSIZ];
  39.  
  40. struct
  41. {
  42.     long    msglen;
  43.     char    from[FROMLEN];
  44.     char    subject[SUBLEN];
  45. } scanl;            /* the line to be printed             */
  46.  
  47. int     sublen;                   /* chars in subject field of scan     */
  48.  
  49. char    username[10];          /* string system name of recipient    */
  50.  
  51. /*  *****************************  MAIN  **********************************/
  52.  
  53. main (argc, argv)
  54. int     argc;
  55. char   *argv[];
  56. {
  57. #ifndef STDALONE
  58.     if (fork () != 0)          /* let ch_local continue ASAP & do    */
  59.     exit (RP_BOK);          /*   actual delivery.                 */
  60.                   /* child will do notification         */
  61. #endif
  62.  
  63.     init(argc,argv);
  64.  
  65.     scanbld ();
  66.  
  67.     alert ();
  68.     exit (0);
  69. }
  70.  
  71. /* */
  72. init(argc,argv)
  73. int argc;
  74. char **argv;
  75. {
  76.     if (argc > 1)
  77.     scanl.msglen = atoi(argv[1]);
  78.     else
  79.     scanl.msglen = -1;
  80.  
  81.     mmdf_init ("ALERT");
  82.     siginit ();
  83.  
  84.     setbuf (stdin, inbuf);
  85.  
  86.     guinfo ();              /* who 'dis?                            */
  87.  
  88.  
  89. }
  90.  
  91. /* */
  92.  
  93. guinfo ()              /* get user name & login directory    */
  94. {
  95.     extern struct passwd *getpwuid ();
  96.     register struct passwd *pwdptr;
  97.     int     realid,
  98.         effecid;
  99.  
  100.     getwho (&realid, &effecid);
  101.     pwdptr = getpwuid (effecid);
  102.  
  103.     strcpy (username, pwdptr -> pw_name);
  104.  
  105. #ifdef DEBUG
  106.     ll_log (logptr, LLOGFTR, "id=%d, u='%s'", effecid, username);
  107. #endif
  108. }
  109. /* */
  110.  
  111. alert ()                     /* send message to user's tty           */
  112. {
  113.     extern int errno;
  114.  
  115.     tty_init();
  116.  
  117.     for (;;)
  118.     {
  119.     if (tty_find (username) < OK)
  120.     {
  121.         if (errno == EBUSY)
  122.         continue;
  123.         else
  124.         break;
  125.     }
  126.  
  127.     fprintf (tty_fp, "\007\r\nNEW:  (");
  128.     if (scanl.msglen < 0)
  129.         fprintf (tty_fp, " ???");
  130.     else
  131.         if (scanl.msglen > 99999)
  132.         fprintf (tty_fp, " BIG");
  133.         else
  134.         fprintf (tty_fp, "%5ld", scanl.msglen);
  135.  
  136.     fprintf (tty_fp, ")  %-15s %s\r\n", scanl.from, scanl.subject);
  137.  
  138.     tty_close ();
  139.     }
  140.  
  141.     tty_end ();
  142.     return;
  143. }
  144. /* */
  145.  
  146. scanbld ()
  147. {                  /* build scan line for tty              */
  148.     char    tempbuf[512];
  149.     register int    len;
  150.     register char   addflag;
  151.  
  152.     sublen = 0;
  153.  
  154.     for (rewind (stdin); ;)
  155.     {                  /* parse the headers                    */
  156.     if (fgets (tempbuf, sizeof tempbuf, stdin) == NULL)
  157.         return;
  158.     len = strlen (tempbuf) - 1;
  159.     if (tempbuf[0] == '\n')
  160.         break;          /* we done hit d' body                  */
  161.     tempbuf[len] = '\0';
  162.  
  163.     if (isspace (tempbuf[0]))
  164.         continue;          /* continuation line                    */
  165.  
  166. #ifdef DEBUG
  167.     ll_log (logptr, LLOGFTR, "1: '%s'", tempbuf);
  168. #endif
  169.     if (equal ("from", tempbuf, 4))
  170.         getfrom (&tempbuf[4]);
  171.     if (equal ("subject", tempbuf, 7))
  172.         getsub (&tempbuf[7]);
  173.     }
  174.  
  175.     addflag = UNSET;          /* not yet added any body to subject    */
  176.     if (sublen < TOSHORT)      /* see if subject should be added to    */
  177.     {                  /*    from the body                     */
  178.     while (sublen < (SUBLEN - 10) &&
  179.         fgets (tempbuf, sizeof tempbuf, stdin) != NULL &&
  180.         ((len = strlen (tempbuf)) - 1) > 0)
  181.     {
  182.         tempbuf[len - 1] = '\0';
  183.                   /* get rid of newline                 */
  184. #ifdef DEBUG
  185.         ll_log (logptr, LLOGFTR, "2:'%s'", tempbuf);
  186. #endif
  187.         if (sublen > 0 && scanl.subject[sublen] != ' ')
  188.                   /* separate from preceding text       */
  189.         scanl.subject[sublen++] = ' ';
  190.  
  191.         if (addflag == UNSET)
  192.         {
  193.         scanl.subject[sublen++] = '(';
  194.         addflag = SET;    /* we now have body text added        */
  195.         }
  196.  
  197.         getsub (tempbuf);
  198.     }
  199.     }
  200.  
  201.     if (addflag == SET)
  202.     strcpy (&(scanl.subject[sublen]), (len > 0) ? "...)" : ")");
  203.     else
  204.     scanl.subject[sublen] = '\0';
  205. }
  206. /* */
  207.  
  208. getfrom (src)                      /*  get name of sender                  */
  209. register char  *src;
  210. {
  211.     register int    n;
  212.     register char  *dest;
  213.  
  214.     while (*src++ != ':');        /* skip over to data                  */
  215.     compress (src, src);
  216.  
  217. #ifdef DEBUG
  218.     ll_log (logptr, LLOGFTR, "getfrom from '%s'", src);
  219. #endif
  220.  
  221.     for (dest = scanl.from, n = (sizeof scanl.from); --n > 0;
  222.         *dest++ = *src++)
  223.     {
  224.     switch (*src)
  225.     {
  226.         case '\0': 
  227.         case '\n': 
  228.         case '(':
  229.         case '<': 
  230.         case '@': 
  231.         case '%':
  232.         break;
  233.  
  234.         case ' ':
  235.         if (equal (" at ", src, 4))
  236.             break;
  237.         default:
  238.         continue;
  239.     }
  240.     break;
  241.     }
  242. }
  243.  
  244. getsub (src)              /* get first part of subject            */
  245. register char  *src;
  246. {
  247.     register char  *dest;
  248.  
  249.     while (isspace (*src))         /* skip over to data                  */
  250.     src++;
  251.     if (*src == ':')
  252.     src++;
  253.     compress (src, src);          /* eliminate leading white space        */
  254.  
  255. #ifdef DEBUG
  256.     ll_log (logptr, LLOGFTR, "getsub from '%s'", src);
  257. #endif
  258.  
  259.     for (dest = &(scanl.subject[sublen]);
  260.         sublen < (SUBLEN - 5) && !isnull (*src);
  261.         *dest++ = *src++, sublen++);
  262. }
  263.